// pages/curriculumScheduling/curriculumScheduling.js
var util = require('../../utils/util');
var App = getApp();
var tr = null;
var countdown = 120;
var settime = function (This) {
    if (countdown == 0) {
        This.setData({
            isLogin: true,
            show_top: false
        })
        countdown = 120;
        return;
    } else {
        This.setData({
            isLogin: false,
            show_top: true,
            click_hq: true,
            last_time: countdown
        })
        countdown--;
    }
    tr = setTimeout(function () {
        settime(This)
    }, 1000)
}
Page({
    /**
     * 页面的初始数据
     */
    data: {
        startDate:'',//开始时间
        endDate:'',
        array: ['全部私教', '中国', '巴西', '日本'],//全部私教
        index: 0,
        page: 1,
        member_list:[],
        array: [],
        membership_id: '',
        course_list: [],
        appointment_account: '',
        account: '',
        consume_account: '',
        unsigned_account: '',
        level:wx.getStorageSync('level'),
        flag_store: true,
        show_top: true, //显示蓝的
        isLogin: true, //是否显示
        code: '',//验证码
        phoneId: '',//
        click_hq: true,
        inputvalue: '',
        end:'',
        start:'',
        level:'',
        course_list1:[]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var This = this;
        This.setData({
            level: wx.getStorageSync('level')
        })
        console.log(This.data.level)
        var time = util.formatTime(new Date());
        var date_value = time.slice(0, 4) + "/" + time.slice(5, 7) + "/" + time.slice(8, 10)
        var date_value1 = time.slice(0, 4) + "-" + time.slice(5, 7) + "-" + time.slice(8, 10)
        // console.log('1')
        This.setData({
            startDate: date_value,
            endDate: date_value,
            start: date_value1,//开始时间
            end: date_value1,
        })
        // console.log('2')
        if(options.res == 2){
            // console.log('进了')
            This.setData({
                start: options.start,
                end: options.end,
                membership_id: options.membership_id, 
                startDate: options.startDate,
                endDate: options.endDate,
            })
        }
        // console.log(This.data.start)
        // console.log()
        This.coach_list();
        This.loading();
    },
    // 开始时间的确定
    bindDateChange: function (e) {
        var This = this;
        var as = e.detail.value;
        var year = as.slice(0,4);
        var month = as.slice(5,7)
        var day = as.slice(8,10)
        This.setData({
            startDate: year+'/'+month+'/'+day,
            page: 1,
            course_list: [],
            course_list1:[],
            start: year + '-' + month + '-' + day
        })
        This.loading();
    },
    // 结束时间的确定
    bindDateChanges: function (e) {
        var This = this;
        var as = e.detail.value;
        var year = as.slice(0, 4);
        var month = as.slice(5, 7)
        var day = as.slice(8, 10);
        This.setData({
            endDate: year + '/' + month + '/' + day,
            page: 1,
            course_list: [],
            course_list1: [],
            end: year + '-' + month + '-' + day
        })
        This.loading();
    },
    // 私教的选择
    bindPickerChange: function (e) {
        var This = this;
        This.setData({
            list: [],
            index: e.detail.value,
            page:1,
            course_list: [],
            membership_id: This.data.array[e.detail.value].id
        })
        This.loading();
    },
    coach_list() {
        var This = this;
        App._get('coach/member/get_coach_list', {
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            phone: wx.getStorageSync('phone'),
            // openid: 1,
            // store_id: 53,
            // phone: 15612340001,
        }, function (res) {
            if (res.status == 1) {
                var arrays = []
                for (var i = 0; i < res.coach_list.length; i++) {
                    arrays.push(res.coach_list[i].coach_name)
                }
                This.setData({
                    member_list: arrays,
                    array: res.coach_list,
                    membership_id: res.coach_list[0].id
                })
                for (var a = 0; a < res.coach_list.length;a++){
                    if (res.coach_list[a].coach_name == '自己'){
                        console.log('进了')
                        This.setData({
                            membership_id: res.coach_list[a].id,
                            index: a,
                        })
                    }
                }
                This.loading();
            } else {
                wx.showToast({
                    title: res.error,
                    image: '../../image/icon_danger.png',
                    duration: 2000
                })
            }
        })
    },
    pro_moclick(){
        this.setData({
            flag_store:true
        })
    },
    // 列表加载
    loading:function(){
        var This = this;
        App._get('coach/Coursearrange/CourseScheduling', {
            store_id: wx.getStorageSync('store_id'),
            openid: wx.getStorageSync('openid'),
            phone: wx.getStorageSync('phone'),
            // store_id: 53,
            // openid: 1,
            // phone: 15612340001,
            start_time: This.data.start,//开始时间（非必填默认今天）
            end_time: This.data.end,//结束时间（非必填）
            coach_id: This.data.membership_id,//私教id（默认全部）
            page: This.data.page,//当前页（非必填默认1）
            limit: 20,//每页显示条数（非必填 默认20）
        }, function (res) {
            if (res.status == 1) {
                // This.setData({
                //   course_list: []
                // })
                if (res.book_data.length > 0) {
                    for (var index in res.book_data) {
                        This.data.course_list.push(res.book_data[index])
                    }
                    This.setData({
                        course_list1: This.data.course_list
                    })
                } else if (This.data.page != 1 && res.book_data.length == '') {
                    wx.showToast({
                        title: '加载完',
                        icon: "none",
                        duration: 1000
                    })
                    This.setData({
                        course_list1: This.data.course_list
                    })
                } 
              console.log(This.data.course_list1)
                This.setData({
                    appointment_account: res.appointment_account,
                    account: res.account,
                    consume_account: res.consume_account,
                    unsigned_account: res.unsigned_account
                })
            } else {
                wx.showToast({
                    title: res.error,
                    image: '../../image/icon_danger.png',
                    duration: 2000
                })
            }
        })
    },
    // 取消预约的接口
    cancels:function(e){
        var This = this;
        This.setData({
            b_id: e.currentTarget.dataset.id
        })
        wx.showModal({
            content: '确认取消预约吗？',
            success(res) {
                if (res.confirm) {
                    App._post_form('coach/personal/abolish_private', {
                        phone: wx.getStorageSync('phone'),
                        openid: wx.getStorageSync('openid'),
                        store_id: wx.getStorageSync('store_id'),
                        id: This.data.b_id
                    }, function (res) {
                        if (res.status != 1) {
                            wx.showToast({
                                title: res.error,
                                icon: 'none',
                                duration: 2000
                            })
                        } else {
                            wx.showToast({
                                title: '取消成功',
                                icon: 'none',
                                duration: 2000
                            })
                            This.setData({
                                course_list:[],
                                course_list1: [],
                                page:1
                            })
                            This.loading();
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    // 去签到
    to_sign:function(e){
        var This = this;
        This.setData({
            flag_store: false,
            click_hq: false,
            isLogin: true,
            show_top: true,
            inputvalue: ''
        })
        if (This.data.b_id != e.currentTarget.dataset.id) {
            clearTimeout(tr)
            countdown = 120;
            This.setData({
                b_id: e.currentTarget.dataset.id
            })
        }
    },
    // 获取短信验证码
    ma_code:function(){
        var This = this;
        console.log(This.data.b_id)
        App._get('api/sijiao/send_jcode', {
            id: This.data.b_id
        }, function (res) {

        })
        clearTimeout(tr)
        This.setData({
            isLogin: (!This.data.isLogin),
            show_top: (!This.data.show_top),
            click_hq: (!This.data.click_hq),
        })
        countdown = 120;
        settime(This);
    },
    // 验证码框的监听事件
    yanzhengInput: function (e) {
        var This = this;
        console.log(e)
        var codes = e.detail.value;
        This.setData({
            code: codes
        })
    },
    //签到的接口
    sign: function (e) {
        var This = this;
        App._post_form('coach/personal/sign_course', {
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            id: This.data.b_id,
            code: This.data.code,
        }, function (res) {
            // console.log(res)
            if (res.status == 0) {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            } else {
                setTimeout(function () {
                    wx.showToast({
                        title: res.success,
                        icon: 'none',
                        duration: 2000
                    })
                    This.setData({
                        flag_store: true,
                        page: 1,
                        course_list: [],
                        course_list1: [],
                    })
                    This.loading();
                }, 1500)

            }
        })
    },
    // 课程规划的点击事件
    to_plan:function(e){
        var This = this;
        wx.navigateTo({
            url: '/pages/curriculum_planning/curriculum_planning?time1=' + e.currentTarget.dataset.time1 + '&name=' + e.currentTarget.dataset.name + '&crouse=' + e.currentTarget.dataset.crouse + '&b_id=' + e.currentTarget.dataset.id + '&time2=' + e.currentTarget.dataset.time2 + '&time3=' + e.currentTarget.dataset.time3 + '&start=' + This.data.start + '&end=' + This.data.end + '&membership_id=' + This.data.membership_id + '&startDate=' + This.data.startDate + '&endDate=' + This.data.endDate,
        })
    },
    // 查看总结
    see_list:function(e){
        var This = this;
        wx.navigateTo({
            url: '/pages/determining_curriculum_planning/determining_curriculum_planning?b_id=' + e.currentTarget.dataset.id + '&start=' + This.data.start + '&end=' + This.data.end + '&membership_id=' + This.data.membership_id + '&startDate=' + This.data.startDate + '&endDate=' + This.data.endDate,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var This = this
        // 页数+1
        This.setData({
            page: This.data.page + 1
        })
        This.loading();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})